﻿using ChestPainCenterSystem.Code.Utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChestPainCenterSystem.Areas.ChestPainCenter.Controllers
{
    public class ImageController : Controller
    {
        //
        // GET: /ChestPainCenter/Image/

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        [HttpPost, AllowAnonymous]
        public JsonResult Upload(string base64)
        {
            var result = string.Empty;
            ConfigurationManager.RefreshSection("appSettings");
            string picVirPath = ConfigurationManager.AppSettings["ImageUploadPath"].ToString();
            var path = Server.MapPath(picVirPath);
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
            DateTime now = DateTime.Now;
            var fileName = string.Format("{0}.{1}", (now.ToString("yyyyMMddHHmmss") + now.Ticks.ToString()), base64.GetExtension());
            var filepath = string.Format("{0}{1}", path, fileName);
            try
            {
                if (base64.ToStream().Thumbnail().Save(filepath))
                    result = string.Format("{0}", fileName);
            }
            catch
            {
                return Json(new { img = "" }); ;
            }
            return Json(new { img = result });
        }
    }
}
namespace System
{
    public static class Expanded
    {
        /// <summary>
        /// 获取 base64 图片字符串的扩展名
        /// </summary>
        /// <param name="inputImgBase64"></param>
        /// <returns></returns>
        public static string GetExtension(this string inputImgBase64)
        {
            var ext = inputImgBase64.Split(';')[0];
            if (string.IsNullOrEmpty(ext)) return "";
            try
            {
                ext = ext.Split(':')[1].Split('/')[1];
            }
            catch (Exception)
            {
                ext = "jpeg";
            }
            return string.IsNullOrEmpty(ext) ? "jpeg" : ext;
        }
        /// <summary>
        /// 将base64 图片字符串 转换为stream
        /// </summary>
        /// <param name="inputImgBase64"></param>
        /// <returns></returns>
        public static Stream ToStream(this string inputImgBase64)
        {
            try
            {
                var inpit = inputImgBase64;
                if (inputImgBase64.StartsWith("data:"))//png
                    inpit = inputImgBase64.Split(',')[1];
                byte[] arr = Convert.FromBase64String(inpit);
                MemoryStream ms = new MemoryStream(arr);
                return ms;
            }
            catch
            {
                return new MemoryStream();
            }
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool Save(this Stream stream, string filePath)
        {
            var path = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            try
            {
                System.Drawing.Image image = Bitmap.FromStream(stream);
                image.Save(filePath);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

